package com.boss.common.base;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.boss.sys.ca.user.dto.UserDTO;
import com.boss.sys.config.CseCommonConfiguration;
import com.boss.sys.utils.SysUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * <br>Title: 通用Controller类
 * <br>Description:通用Controller类
 * <br>Author: 朱孝恒
 * <br>Date: 2019年12月06日 16:27
 */
public class BasicinfoBaseController {
    /**
     * 错误码
     */
    public final static String ERROR_RSCODE = "140999";

    /**
     * 引入接口服务配置类
     */
    @Autowired
    private CseCommonConfiguration urlConfiguration;
    /**
     * 引入RestTemplate
     */
    @Autowired
    private RestTemplate restTemplate;


    public UserDTO getUser(String tokenid) throws Exception {
        try {
            String json = restTemplate.getForObject(urlConfiguration.getSso()+"/v2/user/token/{tokenid}",String.class,tokenid);
            Map<String,String> resultMap = new HashMap<>();
            JSONObject jsonObject = JSON.parseObject(json);
            UserDTO user = new UserDTO();
            if(!SysUtils.isNullOrEmpty(jsonObject) && "100000".equals(jsonObject.getString("rscode"))){
                JSONObject jsondata = jsonObject.getJSONObject("data");
                user.setGuid(jsondata.getString("guid"));
                user.setName(jsondata.getString("name"));
                user.setAgency(jsondata.getString("agency"));
                user.setAgencycode(jsondata.getString("orgcode"));
//                user.setAgencyname(jsondata.getString("orgname"));
                user.setProvince(jsondata.getString("province"));
                user.setYear(jsondata.getInteger("year"));
                user.setAdmdiv(jsondata.getString("admdivname"));
            }else{
                throw new Exception("用户不存在");
            }
            return user;
        } catch (Exception e) {
//            throw new Exception("查询用户信息失败！");
            UserDTO user = new UserDTO();
            user.setProvince("619900000");
            user.setYear(2020);
            return user;
        }
    }
}
